package com.skywolf.chem.tencent.service;

import com.aliyun.oss.ServiceException;
import com.skywolf.chem.live.entity.Videoitem;
import com.skywolf.chem.live.service.IVideoitemService;
import com.skywolf.chem.utils.SysConfig;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.live.v20180801.LiveClient;
import com.tencentcloudapi.live.v20180801.models.DayStreamPlayInfo;
import com.tencentcloudapi.live.v20180801.models.DescribeStreamPlayInfoListRequest;
import com.tencentcloudapi.live.v20180801.models.DescribeStreamPlayInfoListResponse;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.DescribeMediaInfosRequest;
import com.tencentcloudapi.vod.v20180717.models.DescribeMediaInfosResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by pei_pei on 2021/1/22.
 */
@Service
public class LiveInfoService {
    Logger logger = LoggerFactory.getLogger(LiveInfoService.class);
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  // 前五分钟

    /**
     * 获取直播间人数
     * @param liveId
     * @return
     * @throws ServiceException
     */
    public Long getGroupUserCount(Long liveId) throws ServiceException {
        Long userCount = 0L;
        Calendar beforeTime = Calendar.getInstance();
        String secretId = SysConfig._this.getProperty("tencent_cdn.secret_id");
        String secretKey = SysConfig._this.getProperty("tencent_cdn.secret_key");
        String pull_domain = SysConfig._this.getProperty("tencent_cdn.pull_live");
        try{
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("live.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            LiveClient client = new LiveClient(cred, "", clientProfile);
            DescribeStreamPlayInfoListRequest req = new DescribeStreamPlayInfoListRequest();

            beforeTime.add(Calendar.MINUTE, -10);// 10分钟之前的时间
            Date beforeD = beforeTime.getTime();
            req.setStartTime(format.format(beforeD));

            req.setEndTime(format.format(new Date()));
            req.setPlayDomain(pull_domain);
            req.setStreamName(liveId+"");
            DescribeStreamPlayInfoListResponse resp = client.DescribeStreamPlayInfoList(req);
            DayStreamPlayInfo[] list = resp.getDataInfoList();
            if(list!=null){
                userCount = list[list.length-1].getOnline();
            }
        }catch (Exception e){
            logger.error("getGroupUserCount live:"+liveId+" error:{}",e);
        }
        return userCount;
    }

}
